/* Copyright (c) 2008 LepomisLab.com, Andrey Turkin
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "NSCell.h"
#include "NSWindow.h"
#include "NSKeyedUnarchiver.h"

IMPLEMENT_ABSTRACT_CLASS(NSCell, NSObject)

NSCell::NSCell() : NSObject(), _widget(NULL), _widgetParent(NULL), _visible(true)
{
}

void NSCell::_setValue(const wxString& key, wxObject* value)
{
	if (key == wxT("editable"))
	{
		wxVariant *v = wxDynamicCast(value, wxVariant);
		wxASSERT(v);
		_editable = v->GetBool();
	} else if (key == wxT("enabled"))
	{
		wxVariant *v = wxDynamicCast(value, wxVariant);
		wxASSERT(v);
		_enabled = v->GetBool();
		if (_widget)
			_widget->Enable(_enabled);
	} else if (key == wxT("hidden"))
	{
		wxVariant *v = wxDynamicCast(value, wxVariant);
		wxASSERT(v);
		_visible = !v->GetBool();
		if (_widget)
			_widget->Show(_visible);
	} else if (key == wxT("objectValue"))
	{
		if (value)
		{
			wxVariant *v = wxDynamicCast(value, wxVariant);
			wxASSERT(v);
			setContents(v->GetString());
		} else
			setContents(wxT(""));
	} else
		NSObject::_setValue(key, value);
}

wxObject* NSCell::_getValue(const wxString& key)
{
	if (key == wxT("editable"))
	{
		return new wxVariant(_editable);
	} else if (key == wxT("enabled"))
	{
		return new wxVariant(_enabled);
	} else if (key == wxT("hidden"))
	{
		return new wxVariant(!_visible);
	} else if (key == wxT("objectValue"))
	{
		return new wxVariant(getContents());
	} else
		return NSObject::_getValue(key);
}

void NSCell::initWithUnarchiver(NSKeyedUnarchiver *archiver)
{
	NSObject::initWithUnarchiver(archiver);

	wxObject *o = archiver->decodeObjectForKey(wxT("NSContents"));
	if (o)
	{
		wxVariant *s = wxDynamicCast(o, wxVariant);
		if (s)
			_contents = s->GetString();
		else {
			wxASSERT(0);
		}
	}

	if (archiver->contains(wxT("NSCellFlags")))
	{
		unsigned long flags = archiver->decodeIntForKey(wxT("NSCellFlags"));
		setValue(wxT("editable"), !!(flags&0x10000000));
		setValue(wxT("enabled"), !(flags&0x20000000));
	}
}

wxWindow* NSCell::allocWidget(NSView *parent)
{
	wxASSERT(!_widget);
	_widgetParent = parent->widget();
	wxASSERT(_widgetParent);
	_allocWidget(_widgetParent);
	_widget->Enable(_enabled);
	return _widget;
}

void NSCell::releaseWidget(void)
{
	if (_widget)
	{
		_releaseWidget(_widgetParent);
	}
}

wxPoint NSCell::bounds2wxPoint(wxRect *bounds)
{
	wxASSERT(_widgetParent);
	return NSObject::bounds2wxPoint(_widgetParent, *bounds);
}

void NSCell::updateWidget(void)
{
}
